<?php

class Lances_Automaticos_M extends I_Model {

    protected $_table_name = 'lances_automaticos';
    protected $_primary_key = 'ID';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'ID';
    public $rules = array();
    protected $_timestamps = FALSE;

    public function __construct() {
        parent::__construct();
    }


    function all_info($id){
        
        $this->db->select('u.LOGIN,lca.*')
                 ->from($this->_table_name.' as lca')
                 ->join('users as u','u.ID=lca.USER_ID','LEFT')
                 ->where('lca.LOTE_ID',$id)
                 ->order_by('lca.ID','DESC');

        $q = $this->db->get();
        
        if($q->num_rows() == 0){ return false;}
        
        return $q->result();
    }
    
    
    function get_in($in){
        
        $this->db->select('u.LOGIN,lca.*')
        ->from($this->_table_name.' as lca')
        ->join('users as u','u.ID=lca.USER_ID','LEFT')
        ->where_in('lca.LOTE_ID',$in)
        ->order_by('lca.ID','DESC');
        
        $q = $this->db->get();
           
        if($q->num_rows() == 0){ return false;}
         
        return $q->result();
         
    }
    
    function  by_leilao($id){
        $this->db->select('u.LOGIN,lca.*,'
                . '(SELECT COUNT(ID) FROM '.$this->_table_name.' WHERE `READ`=1) as LOADED,'
                . '(SELECT COUNT(ID) FROM '.$this->_table_name.' WHERE `READ`=0) as NO_LOADED'
                )
        ->from($this->_table_name.' as lca')
        ->join('users as u','u.ID=lca.USER_ID')
        ->join('lotes as lt','lt.ID=lca.LOTE_ID','LEFT')
        ->join('leiloes as le','le.ID=lt.LEILAO_ID')
        ->where_in('le.ID',$id)
        ->order_by('lca.ID','DESC');
         
        $q = $this->db->get();
        
        if($q->num_rows() == 0){ return false;}
        
        return $q->result();
    }


    function  resetRead($loteid){

          $this->db->query("UPDATE lances_automaticos SET `READ`='0'  WHERE `LOTE_ID`='$loteid'");
    }


    function update_batch($data){

            foreach ($data as $key => $row) {
                # code...
                $id = $row['ID'];
                unset($row['ID']);
                $this->db->set($row);
                $this->db->where('ID',$id);
                $this->db->update($this->_table_name);
            }
       //$this->db->update_batch($this->_table_name, $data); 

    }
    
}
